package com.lianniu.web.app.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianniu.web.app.dao.IAppVersionControlDao;
import com.lianniu.web.app.service.IAppVersionControlService;
import com.lianniu.web.app.vo.AppVersionControlVo;
import com.lianniu.web.app.vo.SessionRetnVo;
import com.lianniu.web.base.BaseResult;
@Service
public class AppVersionControlServiceImpl implements IAppVersionControlService {

    @Autowired
    private IAppVersionControlDao appVersionControlDao;
    
    /**
     * 查询版本号
     */
    @Override
    public BaseResult getAppVersionControlVo(String version, String sys) {
        String vname=null;
        if("1".equals(sys)){
            vname="tel_android_version";
        }else if("2".equals(sys)){
            vname="tel_ios_version";
        }
        AppVersionControlVo vo = appVersionControlDao.getAppVersionControlVo(vname);
        
        if(upperVersion(version, vo.getVersionCode())){
            vo.setFlag("1");
        }
        if(upperVersion(version, vo.getVersionLowest())){
            vo.setFlagUpdate("1");
        }
        return BaseResult.success(vo);
    }
    @Override
    public BaseResult insertAppVersionControlVo(AppVersionControlVo appVersionControlVo) {
        return BaseResult.success(appVersionControlDao.insertAppVersionControlVo(appVersionControlVo));
    }
    @Override
    public BaseResult updateAppVersionControlVo(AppVersionControlVo appVersionControlVo) {
       
        return BaseResult.success(appVersionControlDao.updateAppVersionControlVo(appVersionControlVo));
    }
    
    private boolean upperVersion(String clientVer, String serverVer) {
        String[] client=clientVer.split("\\.");
        String[] server=serverVer.split("\\.");
        
        for(int i = 0; i < client.length; i++) {
            if (Integer.parseInt(client[i]) < Integer.parseInt(server[i])) {
                return true;
            }
        }
        return false;
    }
}
